package uit.cinemainfo;

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MapActivity extends FragmentActivity {
	public String newline = System.getProperty("line.separator");
	private GoogleMap mMap;
	Intent intent;
	String namecine, addcine, telcine, lat, lng;
	LatLng location;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		
		intent = getIntent();
		namecine = intent.getStringExtra("namecine");
		addcine = intent.getStringExtra("addcine");
		telcine = intent.getStringExtra("telcine");
		lat = intent.getStringExtra("lat");
		lng = intent.getStringExtra("lng");
		location = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
		setUpMapIfNeeded();
		// Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_map, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_showing:
	        	StartShowing();
	            return true;
	        case R.id.menu_coming:
	        	StartComing();
	            return true;
	        case R.id.menu_theater:
	        	StartTheater();
	        	return true;
	        case R.id.menu_news:
	        	StartNews();
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	public void StartComing(){
		Intent intent = new Intent(MapActivity.this, ComingActivity.class);
		startActivity(intent);
	}
	
	public void StartShowing(){
		Intent intent = new Intent(MapActivity.this, ShowingActivity.class);
		startActivity(intent);
	}
	
	public void StartTheater(){
		Intent intent = new Intent(MapActivity.this, TheaterActivity.class);
		startActivity(intent);
	}
	public void StartNews(){
		Intent intent = new Intent(MapActivity.this, ListNewsActivity.class);
		startActivity(intent);
	}
	
	private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment1))
                    .getMap();          
        }
    }
	
	private void setUpMap() {
		float zoomlv = (float) 14.0;
		mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location, zoomlv));
		mMap.getUiSettings().setCompassEnabled(true);
		mMap.addMarker(new MarkerOptions()
	       .position(location)
	       .title(addcine)
	       .snippet(namecine)
	       .draggable(false));
    }
}
